//教室路由
const express = require("express");
const router = express();
const Rooms = require("../model/Rooms")

//添加教室
router.post("/addRoom", async(req, res, next) => {
  const data = await Rooms.create(req.body);
  res.send({
      status: "ok",
      data: data,
  })
});

//分页查询+教室名模糊查询:教室列表
router.get('/listRoom', async(req, res) => {
  page = parseInt(req.query.page);
  limit = parseInt(req.query.limit);
  const total = await Rooms.countDocuments({$or:[{"roomName": { $regex: req.query.roomName ,$options: 'i'} }]})
  const data = await Rooms.find({$or:[{"roomName": { $regex: req.query.roomName ,$options: 'i'} }]}).skip((page - 1) * limit).limit(limit)
  res.send({
      status: "ok",
      data: data,total
  })
})

//教室详情信息
router.get('/detail/:id', async(req, res) => {
  const data = await Rooms.findById(req.params.id)
  res.send({
      status: "ok",
      data: data,
  })
})

//教室信息修改
router.post('/updateRoom/:id', async(req, res) => {
  await Rooms.findByIdAndUpdate(req.params.id, req.body)
  res.send({
      status: "ok",
  })
})

//删除教室
router.delete('/deleteRoom/:id', async(req, res) => {
  await Rooms.findByIdAndDelete(req.params.id)
  res.send({
      status: "ok"
  })
})

//以下为测试接口

//全部列表信息
router.get('/list', async(req, res) => {
  const data = await Rooms.find()
  res.send({
      status: "ok",
      data: data,
  })
})

//删库
router.post('/test', async(req, res) => {
  console.log(req.body)
  await Rooms.deleteMany();
  res.send({
      status: "ok",
  })
})

router.get("/", (req, res) => {
  res.send("hello room...");
});

module.exports = router;